package com.jy.controller.filemanager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jy.common.ajax.AjaxRes;
import com.jy.common.mybatis.Page;
import com.jy.common.utils.base.Const;
import com.jy.controller.base.BaseController;
import com.jy.entity.filemanager.TSProduct;
import com.jy.service.filemanager.product.ProductService;
import com.jy.task.fileoper.Smb2Oper;
import com.jy.task.fileoper.SmbOper;

@Controller
@RequestMapping("/product")
public class ProductController extends BaseController<TSProduct> {

	@Autowired
	private ProductService service;

	/**
	 * @api {post} /product/findBypage  产品分页列表查询
	 * 
	 * @apiName findBypage
	 * @apiGroup product
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct  产品对象
	 */

	@RequestMapping(value = "findBypage", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes index(Page<TSProduct> page, TSProduct o) {
		AjaxRes ar = getAjaxRes();
		try {
			Page<TSProduct> result = service.findByPage(o, page);
			Map<String, Object> p = new HashMap<String, Object>();
			p.put("list", result);
			ar.setSucceed(p);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.DATA_FAIL);
		}
		System.out.println(">>" + ar.getObj());
		return ar;
	}

	/**
	 * @api {post} /product/find  根据id获取产品信息
	 * 
	 * @apiName find
	 * @apiGroup product
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {String} id  产品id
	 */
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes find(String id) {
		AjaxRes ar = getAjaxRes();
		try {
			TSProduct obj = service.getProductById(id);
			ar.setSucceed(obj);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.DATA_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /product/add  产品新增
	 * 
	 * @apiName add
	 * @apiGroup product
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct  产品对象
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes add(TSProduct o) {
		AjaxRes ar = getAjaxRes();
		try {
			o.setId(get32UUID());
			int res = service.creatProduct(o);
			if (res == 1)
				ar.setSucceedMsg(Const.SAVE_SUCCEED);
			else
				ar.setFailMsg(Const.SAVE_FAIL);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.SAVE_FAIL);
		}
		return ar;
	}

	@RequestMapping(value = "checkName", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes checkName(String name) {
		AjaxRes ar = getAjaxRes();
		try {
			List<TSProduct> objList = service.getProListByName(name);
			if (objList.size() > 0) {
				ar.setObj(true);
			} else {
				ar.setObj(false);
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.SAVE_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /product/update  产品修改
	 * 
	 * @apiName update
	 * @apiGroup product
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct  产品对象
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes update(TSProduct o) {
		AjaxRes ar = getAjaxRes();
		try {
			int res = service.updateProduct(o);
			if (res == 1)
				ar.setSucceedMsg(Const.UPDATE_SUCCEED);
			else
				ar.setFailMsg(Const.UPDATE_FAIL);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.UPDATE_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /product/del  产品删除
	 * 
	 * @apiName del
	 * @apiGroup product
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct  产品对象
	 */
	@RequestMapping(value = "del", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes del(TSProduct o) {
		AjaxRes ar = getAjaxRes();
		try {
			int res = service.deleteProduct(o);
			if (res == 1)
				ar.setSucceedMsg(Const.DEL_SUCCEED);
			else
				ar.setFailMsg(Const.DEL_FAIL);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.DEL_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /product/connectSmb  是否能连接共享文件夹
	 * @apiName connectSmb
	 * @apiGroup product
	 * @apiSuccess {Boolean} obj
	 * @apiParam {Object} TSProduct  产品对象
	 */
	@RequestMapping(value = "connectSmb", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes connectSmb(TSProduct o) {
		AjaxRes ar = getAjaxRes();
		boolean result = false;
		try {
			String remoteUrl = "smb://" + o.getUserName() + ":"
					+ o.getPassword() + "@" + o.getSourceAddress() + "";
			if (!remoteUrl.substring(remoteUrl.length() - 1).equals("/")) {
				remoteUrl += "/";
			}
			SmbOper smbOper = new SmbOper(o.getSourceAddress(),
					o.getUserName(), o.getPassword(), "");
			boolean resultSmbConnct = smbOper.tcpConnect();
			if (resultSmbConnct) {
				result = resultSmbConnct;
			} else {
				Smb2Oper objOper = new Smb2Oper(getNewFileIp(
						o.getSourceAddress(), 1), o.getUserName(),
						o.getPassword(), getNewFileIp(o.getSourceAddress(), 2));
				result = objOper.smb2Connect();
			}
		} catch (Exception e) {
		}
		ar.setObj(result);
		return ar;
	}

	private String getNewFileIp(String path, Integer type) {
		String str = "";
		if (path.length() > 0) {
			String[] strPath = path.split("/");
			if (type == 1) {
				str = strPath[0];
			}
			if (type == 2) {
				if (strPath.length > 1) {
					str = strPath[1];
				}
			}
			if (type == 3) {
				if (strPath.length > 2) {
					str = path.substring(path.indexOf(strPath[2])) + "/";
					str = str.replace("//", "/");
				}
			}
		}
		return str;
	}
}
